package com.seeyou.lake.ui

import android.content.Intent
import android.util.Log
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.CommonStatusCodes
import com.seeyou.lake.base.MyBaseActivity
import com.seeyou.lake.databinding.ActivitySplashBinding
import com.seeyou.lake.util.SPUtils
import com.seeyou.lake.viewmodel.SplashViewModel
import com.zackratos.ultimatebarx.ultimatebarx.navigationBar
import com.zackratos.ultimatebarx.ultimatebarx.statusBar

const val SINGN_IN = 12000

class SplashActivityMy : MyBaseActivity<ActivitySplashBinding, SplashViewModel>() {
    override fun myViewModel(): SplashViewModel {
        return SplashViewModel()
    }

    override fun yourViewBing(): ActivitySplashBinding {
        return ActivitySplashBinding.inflate(layoutInflater)
    }

    override fun init() {
        statusBar {
            transparent()
        }
        navigationBar { transparent() }

        val animation1 = ScaleAnimation(1f, 1.2f, 1f, 1.2f)
        animation1.duration = 5000
        mViewBinding.ivSplashTitle.animation = animation1

        val animation = AlphaAnimation(0.3f, 1.0f)
        animation.duration = 2500
        mViewBinding.ivSplashBg.animation = animation
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
                mViewBinding.root.postDelayed({
                    if (SPUtils.getInstance().getObject(SPUtils.SP_USER) == null) {
                        handleLogin()
                    } else {
                        MainActivityMy.open(this@SplashActivityMy)
                        finish()
                    }
                }, 2500)
            }

            override fun onAnimationEnd(animation: Animation?) {

            }

            override fun onAnimationRepeat(animation: Animation?) {

            }

        })


        mVModel.userResponse.observe(this) {
            MainActivityMy.open(this)
            finish()
        }

    }

    private fun handleLogin() {
        val options = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("878511659446-nho6brjsr0psq554o11ceecaehnm9snr.apps.googleusercontent.com")
            .requestId()
            .requestEmail()
            .build()

        val client = GoogleSignIn.getClient(this, options)
        if (client.silentSignIn().isSuccessful) {
            val googleSignInAccount = client.silentSignIn().result
            handleResult(googleSignInAccount)

        } else {
            startActivityForResult(client.signInIntent, SINGN_IN)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == SINGN_IN) {
            try {
                handleResult(
                    GoogleSignIn.getSignedInAccountFromIntent(data)
                        .getResult(ApiException::class.java)
                )

            } catch (e: ApiException) {
                Log.e("---->", "errorCode: " + e.statusCode)
                Log.e("---->", "msg: " + CommonStatusCodes.getStatusCodeString(e.statusCode))
            }
        }
    }

    private fun handleResult(googleSignInAccount: GoogleSignInAccount?) {
        if (googleSignInAccount != null && !googleSignInAccount.isExpired) {
            val email = googleSignInAccount.email
            val userId = googleSignInAccount.id ?: ""
            val displayNames = googleSignInAccount.displayName ?: ""

            val userAvatar = googleSignInAccount.photoUrl.toString()
            if (!email.isNullOrEmpty()) {
                Log.e("--->", "email: $email")
                Log.e("--->", "userId: $userId")
                Log.e("--->", "displayNames: $displayNames")
                Log.e("--->", "userAvatar: $userAvatar")
                Log.e("--->phote", googleSignInAccount.photoUrl.toString())

                mVModel.login(userId, email, userAvatar, displayNames)
            }
        }

    }
}